//
// SoapTrace.Net - A soap message monitoring utility.
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using System;
using System.Runtime.InteropServices;

namespace WinPcap.Tests
{
	/// <summary>
	/// Tests that the code can convert a pcap_addr struct to a 
	/// <see cref="WinPcap.NetworkInterfaceAddress"/> if the pcap_addr struct's netmask is 
	/// set to <see cref="IntPtr.Zero"/>.
	/// </summary>
	/// <remarks>
	/// Note that the WinPcap.Tests assembly has a link to the NativeMethods.cs
	/// which will compile this class into the WinPcap.Tests assembly so we can test this 
	/// internal class which is only inside the WinPcap assembly itself.
	/// </remarks>
	[ TestFixture ]
	public class PcapAddressWithNullNetMaskTestFixture
	{		
		NativeMethods.pcap_addr PcapAddress;
		NetworkInterfaceAddress Address;
		NativeMethods.sockaddr_in SocketAddress;
		
		/// <summary>
		/// Initialises this test fixture.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			// Init the socket address.
			
			SocketAddress.sin_family = NativeMethods.AddressFamily.AF_INET;
			SocketAddress.sin_port = 3000;
			SocketAddress.addr0 = 200;
			SocketAddress.addr1 = 1;
			SocketAddress.addr2 = 1;
			SocketAddress.addr3 = 1;
			
			IntPtr SocketAddressPtr = Marshal.AllocCoTaskMem( Marshal.SizeOf( SocketAddress ) );
			Marshal.StructureToPtr( SocketAddress, SocketAddressPtr, false );
			
			// Init the pcap_addr struct.
		
			PcapAddress.address = SocketAddressPtr;
			PcapAddress.broadcastaddr = IntPtr.Zero;
			PcapAddress.dstaddr = IntPtr.Zero;
			PcapAddress.netmask = IntPtr.Zero;
			PcapAddress.next = IntPtr.Zero;
			
			// Convert the pcap_addr to a NetworkInterfaceAddress.
			
			Address = PcapAddress.ToNetworkInterfaceAddress( );
			
			// Clean up.
			
			Marshal.DestroyStructure( SocketAddressPtr, typeof( NativeMethods.sockaddr_in ) );
		}
		
		/// <summary>
		/// Checks that the conversion actually produced an object.
		/// </summary>
		[ Test ]
		public void NetworkInterfaceAddressObjectIsNotNull( )
		{
			Assert.IsNotNull( Address );
		}
		
		/// <summary>
		/// The <see cref="WinPcap.NetworkInterfaceAddress.Address"/> should not be 
		/// <see langword="null"/>.
		/// </summary>
		[ Test ]
		public void AddressIsNotNull( )
		{
			Assert.IsNotNull( Address.Address );
		}

		/// <summary>
		/// The <see cref="WinPcap.NetworkInterfaceAddress.BroadcastAddress"/> should be 
		/// <see langword="null"/>.
		/// </summary>
		[ Test ]
		public void BroadcastAddressIsNull( )
		{
			Assert.IsNull( Address.BroadcastAddress );
		}
		
		/// <summary>
		/// The <see cref="WinPcap.NetworkInterfaceAddress.NetMask"/> should be 
		/// <see langword="null"/>.
		/// </summary>
		[ Test ]
		public void NetMaskIsNull( )
		{
			Assert.IsNull( Address.NetMask );
		}		
	}
}
